<template>
  <div >
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
    <el-form ref="form" :rules="rules" :model="form" label-width="120px" class="layout-all">
      <el-form-item label="社区名称:" prop="title">
        <el-input v-model="form.title" class="form-width" placeholder="请输入社区名称" />
      </el-form-item>
      <el-form-item label="团长姓名:" prop="team_title">
        <el-input v-model="form.team_title" class="form-width" placeholder="请输入团长姓名" />
      </el-form-item>
      <el-form-item label="团长电话:" prop="team_phone">
        <el-input v-model="form.team_phone" class="form-width" placeholder="请输入团长电话" />
      </el-form-item>
      <el-form-item label="指定用户:" prop="team_id">
        <el-select v-model="form.team_id" class="form-width" clearable filterable placeholder="请选择用户">
          <el-option
            v-for="item in user_list"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分润比:" prop="divide_ratio">
        <el-input v-model="form.divide_ratio" class="form-width" placeholder="请输入分润比" />
      </el-form-item>
      <el-form-item label="一级佣金比(%):" prop="commission1">
        <el-input v-model="form.commission1" class="form-width" placeholder="请输入一级佣金比" />
      </el-form-item>
      <el-form-item label="二级佣金比(%):" prop="commission2">
        <el-input v-model="form.commission2" class="form-width" placeholder="请输入二级佣金比" />
      </el-form-item>
      <el-form-item label="团队等级:" prop="level">
        <el-input v-model="form.level" class="form-width" placeholder="请输入等级" />
      </el-form-item>
      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name:"team_manage_form",
  data() {
    return {
      id: 0,
      is_change: undefined,
      user_list: [],
      form: {
        status:true
      },
      rules: {
        title: [
          { required: true, message: '请输入社区名称', trigger: 'blur' }
        ],
        team_title: [
          { required: true, message: '请输入团长姓名', trigger: 'blur' }
        ],
        team_phone: [
          { required: true, message: '请输入团长电话', trigger: 'blur' }
        ],
        team_id: [
          { required: true, message: '请选择指定用户', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '请输入等级', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.init_into()
  },
  methods: {
    // 提交
    onSubmit(formName) {
      const form = this.form
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/admin/Team/save', form, 'POST').then(e => {
            this.$back()
          })
          
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      this.get('/admin/Team/info?id=' + this.id, 'POST').then(e => {
        this.user_list = e.user_list
        if(this.id>0){
          this.form = e.info.tpl
          this.form.status =this.form.status== 1
        }
       setTimeout(()=>{this.is_change=false},100)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
        this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
